package cn.dengta.webapp.im.entity;

import java.util.*;

import cn.dengta.common.model.BizFlag;
import cn.dengta.common.model.ValueProxy;
import cn.dengta.context.model.LabelI18nProxy;
import cn.dengta.webapp.base.entity.TimeBasedEntity;
import cn.dengta.webapp.user.model.Gender;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import me.codeplayer.util.NumberUtil;
import me.codeplayer.util.X;
import org.apache.commons.lang3.tuple.Pair;

import static cn.dengta.webapp.im.enums.UserActiveQuota.*;

/**
 * 用户活跃奖励
 *
 * @author XiaoRong
 * @since 2022/12/27
 */
@Getter
@Setter
@Accessors(chain = true)
@TableName("im_user_active_reward")
public class UserActiveReward extends TimeBasedEntity {

	/** 活动日期 */
	private Date actDate;
	/** 用户ID */
	private Long userId;
	/** 性别：0=女；1=男； */
	private Gender gender;
	/** 回复数 */
	private Integer replyTimes;
	/** 收到的消息数 */
	private Integer msgTimes;
	/** 搭讪次数 */
	private Integer greetTimes;
	/** 在线时长 */
	private Integer onlineTime;
	/** 聊天场数 */
	private Integer chatTimes;
	/** 视频通话时长 */
	private Integer videoTimes;
	/** 任务状态：0=未完成；1=已完成 */
	private Integer status;
	/** 奖励状态：0=未发放；1=已发放 */
	private Integer rewardStatus;
	/** 总奖励数量 */
	private Integer rewardNum;
	/** 基础奖励数量 */
	private Integer baseRewardNum;
	/** 视频奖励数量 */
	private Integer videoRewardNum;
	public static final String ACT_DATE = "act_date";
	public static final String GENDER = "gender";
	public static final String REWARD_STATUS = "reward_status";
	public static final String USER_ID = "user_id";

	/**
	 * 转换活跃数据 Pair<任务标记，用户活跃数据>
	 */
	public static Pair<Long, UserActiveReward> parseQuota(List<String> values, boolean parseOnlineTime) {
		if (X.isValid(values) && values.size() == CACHE.length) {
			// 获取任务标记
			long flags = NumberUtil.getLong(values.get(TASK.ordinal()), 0);
			Integer onlineTime = ONLINE.getQuotaValue(values);
			UserActiveReward activeReward = new UserActiveReward()
					.setReplyTimes(REPLY.getQuotaValue(values))
					.setMsgTimes(MSG.getQuotaValue(values))
					.setGreetTimes(GREET.getQuotaValue(values))
					.setOnlineTime(parseOnlineTime ? getOnlineMinute(onlineTime) : onlineTime)
					.setVideoTimes(getOnlineMinute(VIDEO.getQuotaValue(values)))
					.setChatTimes(CHAT.getQuotaValue(values))
					.setBaseRewardNum(0)
					.setVideoRewardNum(0);
			return Pair.of(flags, activeReward);
		}
		return Pair.of(0L, new UserActiveReward().setGreetTimes(0).setChatTimes(0).setReplyTimes(0).setOnlineTime(0).setMsgTimes(0));
	}

	public static Integer getOnlineMinute(Integer onlineTime) {
		if (onlineTime != null && onlineTime > 0) {
			int m = onlineTime % 60;
			return m > 0 ? (onlineTime / 60) + 1 : onlineTime / 60;
		}
		return 0;
	}

	@Getter
	public enum UserActiveRewardTask implements LabelI18nProxy<UserActiveRewardTask, Integer> {

		/** 搭讪任务 */
		GREET(1, "主动搭讪女用户", "今日撩汉次数", "去搭讪", "home_chat_list"),
		/** 回复任务 */
		REPLY(2, "回复女用户的消息", "今日有效回复率", "去回复", "home_chat_list"),
		/** 在线任务 */
		ONLINE_TIME(3, "今日在线时长", "今日在线时长", "去聊天", "home_chat_list"),
		/** 视频通话 */
		VIDEO(4, "今日视频时长", "今日视频时长", "去视频", "home_chat_list");

		/** 任务id */
		public final Integer value;
		/** 任务名称 */
		public final EnumMap<Gender, String> nameMap;
		/** 男用户任务名称 */
		public final String maleName;
		/** 女用户任务名称 */
		public final String femaleName;
		/** 按钮名称 */
		public final String buttonName;
		/** 跳转链接 */
		public final String url;
		/** 任务标记 */
		public final long bizFlag;

		public final ValueProxy<UserActiveRewardTask, Integer> proxy;

		UserActiveRewardTask(Integer value, String maleName, String femaleName, String buttonName, String url) {
			this.value = value;
			this.maleName = maleName;
			this.femaleName = femaleName;
			this.buttonName = buttonName;
			this.url = url;
			this.bizFlag = 1L << ordinal();
			EnumMap<Gender, String> map = new EnumMap<>(Gender.class);
			map.put(Gender.MALE, maleName);
			map.put(Gender.FEMALE, femaleName);
			this.nameMap = map;
			this.proxy = new ValueProxy<>(this, value);
		}

		public static final UserActiveRewardTask[] CACHE = values();

		@Override
		public ValueProxy<UserActiveRewardTask, Integer> getProxy() {
			return proxy;
		}

		public static UserActiveRewardTask[] getValues() {
			return ValueProxy.getCachedArray(UserActiveRewardTask.class, UserActiveRewardTask::values);
		}

		public static UserActiveRewardTask of(Integer value) {
			UserActiveRewardTask task = GREET.getValueOf(value);
			return task == null ? GREET : task;
		}

		public boolean asBool(final long flags) {
			return BizFlag.hasFlag(flags, this.bizFlag);
		}

		public long toggleFlag(final long flags, boolean addOrRemove) {
			return BizFlag.WritableBizFlag.toggleFlag(flags, bizFlag, addOrRemove);
		}

		public static boolean isFinish(final long flags, Gender gender) {
			boolean basicsFinish = isBaseFinish(flags, gender);
			// 完成搭讪、在线时长 或 视频通话任务
			if (!basicsFinish) {
				return isVideoFinish(flags);
			}
			return true;
		}

		/**
		 * 是否完成基础任务
		 */
		public static boolean isBaseFinish(final long flags, Gender gender) {
			// 女用户需要完成搭讪、在线时长 或 视频通话任务
			if (Gender.FEMALE == gender) {
				return (GREET.asBool(flags) && ONLINE_TIME.asBool(flags));
			}
			return GREET.asBool(flags) && REPLY.asBool(flags) && ONLINE_TIME.asBool(flags);
		}

		/**
		 * 是否完成基础任务
		 */
		public static boolean isVideoFinish(final long flags) {
			return VIDEO.asBool(flags);
		}
	}

}
